<?php
require_once('../../../config.php');
require_once($CFG->libdir . '/formslib.php');
require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->dirroot . '/local/blackboxsurvey/lib.php');
require_once($CFG->dirroot . "/local/blackboxsurvey/forms.php");
define('AJAX_SCRIPT', true);

//yeu cau login,sau khi login tu dong chuyen sang trang chu
global $USER;
if ($USER->id == null){
    $SESSION->homepage = true;
    require_login($SITE);
}
if($SESSION->homepage){
    $SESSION->homepage = null;
    redirect( $CFG->wwwroot);
}
//class
require_once($CFG->dirroot . "/local/blackboxsurvey/class/survey.php");
require_once($CFG->dirroot . "/local/blackboxsurvey/class/questionaire.php");
require_once($CFG->dirroot . "/local/blackboxsurvey/class/matching.php");
require_once($CFG->dirroot . "/local/blackboxsurvey/class/questions.php");
require_once($CFG->dirroot . "/local/blackboxsurvey/class/question_choice.php");

//path
$path = $CFG->wwwroot.'/local/blackboxsurvey/matcher/manage_bbsurvey.php';
$path_image = $CFG->wwwroot .'/local/blackboxsurvey/image/';
$array['path_image'] = $path_image;
try{
$id_bbsurvey = required_param('id_bbsurvey', PARAM_TEXT);
$id_subject = required_param('id_subject', PARAM_TEXT);
$check_admin = required_param('s', PARAM_TEXT);

$c_qn = new b_questionaire();
$c_match = new b_matching();
$c_survey = new b_survey();
$b_ques = new b_questions(); 
$b_ques_choice = new b_question_choice();
//tao id cho matching
$id_matching = $c_match->create_matching_id($id_bbsurvey,$id_subject);
$array['id_matching'] = $id_matching;
$array['check_admin'] = $check_admin;
//xoa cac matching data chua save
$c_match->delete_unsaved_matching_data($id_matching);
//cho nay goi cai dong ham lay chi tiet phieu khao sat
$survey = $c_qn->get_questionaire_info($id_bbsurvey,$id_subject);//lay thong tin co ban cua phieu khao sat

//gan nhung du lieu vao $arry de hien tren form
$array['program'] = $survey->program_name;
$array['subject'] = $survey->subject_name_vn;
$array['start'] = $survey->date_start;
$array['end'] = $survey->date_end;
//$array['id_program'] = $survey->p_id;

//lay danh sach phieu khao sat cua subject
$qns = $c_qn->get_questionaire_by_IDbbsurvey_IDsubject($id_bbsurvey, $id_subject);
$array['num_qn'] = count($qns);
$count = 1;
foreach($qns as $qn)
{
    $array['qn_id'.$count] = $qn->id;
    $array['surveyor'.$count] = $qn->firstname.' '.$qn->lastname;
    $count++;
}
//lay output cua subject
$array['matching_content'] = $c_match->get_matching_outputs($id_bbsurvey, $id_subject);
//lay accepted output request
$array['accepted_request'] = $c_match->show_accepted_output_request($qns);
//lay cau hoi cua phieu khao sat
$questions = $c_survey->get_survey_question_bySurveyID($id_bbsurvey);
$array['ques_num'] = count($questions);
$count = 1;

//lay cau hoi da add vao survey
foreach($questions as $q) {
    $q_content = preg_replace(array('#<p>#', '#</p>#'), '', $q->content, 1);
    $array['ques_id'.$count] = $q->id;
    $array['ques_content'.$count] = $count.'- '.$q_content;
    $array['type'.$count] = $q->id_type;
    //lay cau tra loi cua tung cau hoi cua tung phieu khao sat
    foreach($qns as $qn)
    {
        $ans = $c_qn->view_question_answer($qn->id, $q->id, $q->id_type);
        if($q->id_type == 1 || $q->id_type == 4)
        {
            $array['ans'.$qn->id.$count] = $ans->answer_content;
        }
        elseif($q->id_type == 2 || $q->id_type==  3)
        {
            //goi ham lay tat ca choice cua cau hoi
            $ques_choice = $b_ques_choice->get_choice_by_question($q->ques_id);
            //dem so luong option
            $array['num_choice'.$count] = count($ques_choice);
            //chay foreach de gan choice vao array
            $n = 1;
            foreach($ques_choice as $c){
                $array['choice_id'.$count.$n] = $c->id;
                $array['choice_content'.$count.$n] = $c->content;
                //chay for de kiem tra choice co dc chon chua
                foreach($ans as $a){
                    if($c->id == $a->id_choice)
                        $array['choice_ck'.$qn->id.$count.$n] = 'checked';
                    else
                        $array['choice_ck'.$qn->id.$count.$n] = '';
                }
                $n++;
            }
        }      
    }
    //echo "<script  type='text/javascript'>alert('".$qn_id.','.$q->id.','.$q->id_type.','."');</script>";
    $count++;  
}
}catch(Exception $ex){}


//Them thanh menu ngang ung voi trang trong duong link
navigation_node::override_active_url(new moodle_url($path, array('param'=>'value')));

/* Chuan bi trang web can hien thi */	


$url = new moodle_url($path);
$PAGE->set_url($url);
$PAGE->set_title('Matching'.get_string('subject', 'local_blackboxsurvey'));
$PAGE->set_heading('Matching'.get_string('subject', 'local_blackboxsurvey'));
$PAGE->set_pagelayout('standard');

echo $OUTPUT->header();

$mform = new form_matching_subject(null, $array);
/* Viet doan code cua ban o day */
if ($fromform = $mform->get_data()) 
{	
    $c_mat = new b_matching();
    $id_matching = $fromform->id_matching;
    $check_admin = $fromform->check_admin;
    //save status cua matched output
    $c_mat->update_matched_output_status($id_matching);
    
    //update status cua matching
    $c_mat->update_matching_subject_status($id_matching);
    
    //save course map data
    $c_mat->save_course_map_ByMatching_subject($id_matching);
    
    //save logs
    global $USER;
    $c_mat->save_logs($id_matching, 'Begin match', $USER->id);
    
    //chuyen den trang manage_questionaire.php
    echo '<script>location.href="'.$path.'?s='.$check_admin.'"</script>';
}

$mform->display();
/*------Ket thuc doan code cua ban-------*/

echo $OUTPUT->footer();